package com.abcd.interceptor;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.modules.network.NetworkInterceptorCreator;
import com.facebook.react.modules.network.NetworkingModule;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.shell.MainReactPackage;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Provider;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * 自定义的MainReactPackage工具包
 * 1. 移除了官方添加的NetworkingModule模块
 * 2. 添加了带有拦截器的自定义的NetworkingModule模块
 * 3. 添加'com.squareup.okhttp3:logging-interceptor:3.10.0'
 * 4. 实现了网络访问时的日志打印
 * Created by mazaiting on 2018/6/13.
 */

public class CustomMainReactPackage_Java extends MainReactPackage {
  
  /**
   * 获取日志拦截器
   *
   * @return Http日志拦截器
   */
  private static HttpLoggingInterceptor getHttpLoggingInterceptor() {
    // 日志显示级别
    HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
    // 新建拦截器
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
      @Override
      public void log(@NonNull String message) {
        // 判断是否有内容
        if (!TextUtils.isEmpty(message)) {
          // 打印日志
          Log.e("Okhttp3 =====>", message);
        }
      }
    });
    // 设置显示级别
    loggingInterceptor.setLevel(level);
    return loggingInterceptor;
  }
  
  /**
   * 重写NativeModules模块
   *
   * @param context 上下文
   */
  @Override
  public List<ModuleSpec> getNativeModules(ReactApplicationContext context) {
    List<ModuleSpec> nativeModules = super.getNativeModules(context);
    // 返回整改后的Modules列表
    return adjustModules(context, nativeModules);
  }

  /**
   * 修改系统的Modules
   *
   * @param context       上下文
   * @param nativeModules 本地模块
   * @return 模块列表
   */
  private List<ModuleSpec> adjustModules(final ReactApplicationContext context, List<ModuleSpec> nativeModules) {
    // 创建拦截器列表，NetworkInterceptorCreator在com.facebook.react.modules.network包下
    final List<NetworkInterceptorCreator> list = new ArrayList<>();
    // 添加拦截器
    list.add(new NetworkInterceptorCreator() {
      @Override
      public Interceptor create() {
        // 返回自定义的拦截器
        return getHttpLoggingInterceptor();
      }
    });
    // 创建模块列表
    List<ModuleSpec> modules = new ArrayList<>(nativeModules);
    // 遍历模块
    for (ModuleSpec moduleSpec : nativeModules) {
      // 判断是否为NetworkingModule网络模块
      if (NetworkingModule.class.equals(moduleSpec.getType())) {
        modules.remove(moduleSpec);
        // 添加自定义的网络模块
        modules.add(ModuleSpec.nativeModuleSpec(
                NetworkingModule.class,
                new Provider<NativeModule>() {
                  @Override
                  public NativeModule get() {
                    // 返回带有拦截器的NetworkingModule网络模块
                    return new NetworkingModule(context, list);
                  }
                }));
        break;
      }
    }
    
    return modules;
  }
}
